/**
 * @author Diego de Blas Mateo
 */

function Player(params)
{
	this.id = null
	this.stepX = 5;
	this.stepY = 5;
	this.posX = 0;
	this.posY = 0;
	this.width = 10;
	this.height = 10;
	
	//Test
	this.color = "rgb("+rndVal(0,255)+", "+rndVal(0,255)+", "+rndVal(0,255)+")";

	//Sets the parameters specified in the creation
	if (params !== undefined && typeof(params) == "object")
	{
		this.id = params.id;
		
		if (params.position !== undefined && typeof(params.position) == "object")
			this.setPosition(params.position.x, params.position.y);
		if (params.width !== undefined)
			this.width = params.width;
		if (params.height !== undefined)
			this.height = params.height;
	}
}

Player.prototype.assignId = function(id)
{
	this.id = id;
}

Player.prototype.paint = function ()
{
	var context = Bomberman.drawingContext;
	if (context)
	{
		context.fillStyle = this.color;
		context.fillRect (this.posX, this.posY, this.width, this.height);
	}
}

Player.prototype.getPosition = function ()
{
	var position = {}
	position.x = this.posX;
	position.y = this.posY;
	
	return position;
}

Player.prototype.setPosition = function (x, y)
{
	this.posX = x;
	this.posY = y;
}

Player.prototype.moveLeft = function (step)
{
	var stepLeft = (step !== undefined) ? step : this.stepX;
	
	this.posX -= stepLeft;
	if (this.posX < 0) this.posX = 0;
	
	if (Bomberman.onMove !== null) Bomberman.onMove(this.id, this.posX, this.posY);
}

Player.prototype.moveRight = function (step)
{
	var stepRight = (step !== undefined) ? step : this.stepX;
	
	this.posX += stepRight;
	if ((this.posX + this.width) > Bomberman.screenWidth) this.posX = Bomberman.screenWidth - this.width;
	
	if (Bomberman.onMove !== null) Bomberman.onMove(this.id, this.posX, this.posY);
}

Player.prototype.moveUp = function (step)
{
	var stepUp = (step !== undefined) ? step : this.stepY;
	
	this.posY -= stepUp;
	if (this.posY < 0) this.posY = 0;
	
	if (Bomberman.onMove !== null) Bomberman.onMove(this.id, this.posX, this.posY);
}

Player.prototype.moveDown = function (step)
{
	var stepDown = (step !== undefined) ? step : this.stepY;
	
	this.posY += stepDown;
	if ((this.posY + this.height) > Bomberman.screenHeight) this.posY = Bomberman.screenHeight - this.height;
	
	if (Bomberman.onMove !== null) Bomberman.onMove(this.id, this.posX, this.posY);
}